
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Configuration;
using System.Diagnostics;

namespace Swaf.Instrumentation
{
	/// <summary>
	/// Summary 
	/// description for BaseCounter.
	/// </summary>
	public class BaseCounter : IPerformanceCounter
	{
		protected PerformanceCounterCategory m_counterCategory;
		protected string m_categoryName;
		protected string m_description;
		protected string m_counterName;
		protected bool m_counterCreated=false;
		protected PerformanceCounterCategory m_counterCatagory;
		protected PerformanceCounter m_primaryCounter;
		protected CounterCreationDataCollection m_CCDC;
		protected string m_lockValue="";

		public BaseCounter(IApplication app, string name)
		{
			m_categoryName= app.resMgr.getMacro("ApplicationId");
			m_counterName = name;
			m_description = app.resMgr.getMacro("ApplicationDescription");
		}
		#region IPerformanceCounter Members
		public string categoryName
		{
			get
			{
				return m_categoryName;
			}
			set
			{
				m_categoryName=value;
			}
		}
		public string counterName{get{return m_counterName;}}

		public string name
		{
			get{return m_counterName;}
		}
		public virtual long currentValue
		{
			get
			{
				return m_primaryCounter.RawValue;
			}
			set
			{
				m_primaryCounter.RawValue=value;
			}
		}
		public virtual bool createCounters(CounterCreationDataCollection ccdc)
		{
			m_counterCreated=!counterExists(m_counterName,m_categoryName);
			CounterCreationData currentNum = new CounterCreationData();
			currentNum.CounterType = PerformanceCounterType.NumberOfItems32;
			currentNum.CounterName = m_counterName;
			ccdc.Add(currentNum);
			return m_counterCreated;
			
		}
		protected bool counterExists(string counterName,string categoryName)
		{
			if (PerformanceCounterCategory.Exists(categoryName))
				return PerformanceCounterCategory.CounterExists(counterName,categoryName);
			else
				return false;
		}
		public virtual void initCounters()
		{

			lock(m_lockValue)
			{
				PerformanceCounter p = new PerformanceCounter(m_categoryName, m_counterName, false);
				p.RawValue = 0;
				m_primaryCounter = p;
			}
		}
		public virtual void increment()
		{
			lock(m_lockValue)
			{
				m_primaryCounter.Increment();
			}
		}

		public virtual void incrementBy(long incValue)
		{
			lock(m_lockValue)
			{
				m_primaryCounter.IncrementBy(incValue);
			}
		}

		public virtual void decrement()
		{
			lock(m_lockValue)
			{
				m_primaryCounter.Decrement();
			}
		}


		public virtual void decrementBy(long decValue)
		{
			lock(m_lockValue)
			{
				m_primaryCounter.RawValue -= decValue;
			}
		}


		#endregion
	}
}